package fact.view.controller;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ValueChangeEvent;
import javax.servlet.http.HttpSession;

import fact.view.DTO.EntrySetDTO;
import fact.view.DTO.NodeHierarchyDTO;
import fact.view.DTO.ScenarioDTO;
import fact.view.model.EntrySetModelBean;
import fact.view.model.NodeHierarchyModelBean;
import fact.view.model.ScenarioModelBean;
import fact.view.util.ServletUtil;

@ManagedBean(name = "entrySetBean")
@ViewScoped
public class EntrySetBean extends ServletUtil implements Serializable
{
	private static final long serialVersionUID = 5955362608874150481L;
	
	@ManagedProperty(value="#{entrySetModelBean}")
	private EntrySetModelBean entrySetModelBean;	
	
	@ManagedProperty(value="#{nodeHierarchyModelBean}")
	private NodeHierarchyModelBean nodeHierarchyModelBean;	
	
	@ManagedProperty(value="#{scenarioModelBean}")
	private ScenarioModelBean scenarioModelBean;	
	
	ArrayList<EntrySetDTO> entrySetList=new  ArrayList<EntrySetDTO>();
    HttpSession session = (HttpSession) FacesContext.getCurrentInstance().getExternalContext().getSession(false);
    
	private int month;
	private int year;
	private int makeId;
	private int marketId;
	private int segmentId;
	
	public ArrayList<EntrySetDTO> getEntrySetList() {
    	
		if(month==0 || month ==9999 || year==0 || year==9999)
		{
			
		}
		else
		{
		try{
			
			if(session.getAttribute("session_entrySetList"+month+year)==null)
			{
				entrySetList=entrySetModelBean.getEntrySetList(month, year);
				session.setAttribute("session_entrySetList"+month+year, entrySetList);
			}
			else
				entrySetList=(ArrayList<EntrySetDTO>)session.getAttribute("session_entrySetList"+month+year);
		   }
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		}
		
		
		return entrySetList;
	}
	public void createEntrySet() throws SQLException
	{
		FacesContext fc = FacesContext.getCurrentInstance();		
		this.makeId = Integer.valueOf(getRequestParam(fc,"makeId"));
		this.marketId = Integer.valueOf(getRequestParam(fc,"marketId"));
		this.segmentId = Integer.valueOf(getRequestParam(fc,"segmentId"));
		
		
		EntrySetDTO entrySet=new EntrySetDTO();
		entrySet.setMakeId(makeId);
		entrySet.setMarketId(marketId);
		entrySet.setSegmentId(segmentId);
		entrySet.setMonth(this.month);
		entrySet.setYear(this.year);
		
		ArrayList<NodeHierarchyDTO> nodeList=new ArrayList<NodeHierarchyDTO>();
		ArrayList<ScenarioDTO> scenarioList=new ArrayList<ScenarioDTO>();
		
		nodeList=nodeHierarchyModelBean.getAllKpiNodeList();
		scenarioList=scenarioModelBean.getScenarioList();
		
		boolean res=entrySetModelBean.createEntrySet(entrySet,nodeList,scenarioList);
		if(res==true)
		{
			entrySetList=entrySetModelBean.getEntrySetList(month, year);
			session.setAttribute("session_entrySetList"+month+year, entrySetList);
		}
			
	}
	
	public void batchEntrySet() throws SQLException
	{
		ArrayList<NodeHierarchyDTO> nodeList=new ArrayList<NodeHierarchyDTO>();
		ArrayList<ScenarioDTO> scenarioList=new ArrayList<ScenarioDTO>();
		
		nodeList=nodeHierarchyModelBean.getAllKpiNodeList();
		scenarioList=scenarioModelBean.getScenarioList();
		
		boolean res=entrySetModelBean.batchEntrySet(entrySetList,nodeList,scenarioList,this.month,this.year);
		if(res==true)
		{
			entrySetList=entrySetModelBean.getEntrySetList(month, year);
			session.setAttribute("session_entrySetList"+month+year, entrySetList);
		}
		
			
	}

	public void toggleMonth(ValueChangeEvent e) 
	{
		try
		{
		this.month=Integer.parseInt(e.getNewValue().toString());
		getEntrySetList();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	public void toggleYear(ValueChangeEvent e) 
	{
		try
		{
		this.year=Integer.parseInt(e.getNewValue().toString());
		getEntrySetList();
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
	}
	
	public EntrySetModelBean getEntrySetModelBean() {
		return entrySetModelBean;
	}

	public void setEntrySetModelBean(EntrySetModelBean entrySetModelBean) {
		this.entrySetModelBean = entrySetModelBean;
	}

	public HttpSession getSession() {
		return session;
	}

	public void setSession(HttpSession session) {
		this.session = session;
	}

	public int getMonth() {
		return month;
	}

	public void setMonth(int month) {
		this.month = month;
	}

	public int getYear() {
		return year;
	}

	public void setYear(int year) {
		this.year = year;
	}

	public void setEntrySetList(ArrayList<EntrySetDTO> entrySetList) {
		this.entrySetList = entrySetList;
	}
	public int getMakeId() {
		return makeId;
	}
	public void setMakeId(int makeId) {
		this.makeId = makeId;
	}
	public int getMarketId() {
		return marketId;
	}
	public void setMarketId(int marketId) {
		this.marketId = marketId;
	}
	public int getSegmentId() {
		return segmentId;
	}
	public void setSegmentId(int segmentId) {
		this.segmentId = segmentId;
	}
	public NodeHierarchyModelBean getNodeHierarchyModelBean() {
		return nodeHierarchyModelBean;
	}
	public void setNodeHierarchyModelBean(
			NodeHierarchyModelBean nodeHierarchyModelBean) {
		this.nodeHierarchyModelBean = nodeHierarchyModelBean;
	}
	public ScenarioModelBean getScenarioModelBean() {
		return scenarioModelBean;
	}
	public void setScenarioModelBean(ScenarioModelBean scenarioModelBean) {
		this.scenarioModelBean = scenarioModelBean;
	}
	
}
